package com.metaera.app;

import android.app.Application;
import android.content.Context;
import android.util.Log;


import com.metaera.app.BuildConfig;
import com.metaera.app.controller.presenter.UserInfoPresenter;
import com.metaera.app.utils.ActivityNavigateManager;
import com.metaera.app.utils.PushHelper;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.commonsdk.utils.UMUtils;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.api.UPushRegisterCallback;
import com.umeng.socialize.PlatformConfig;
import com.umeng.umcrash.UMCrash;
import com.umeng.umcrash.UMCrashCallback;

import androidx.multidex.MultiDex;
import io.reactivex.plugins.RxJavaPlugins;

public class MetaEraApplication extends Application {

    static {

        SmartRefreshLayout.setDefaultRefreshInitializer((context, layout) ->
                layout.setReboundDuration(300)
                        .setEnableAutoLoadMore(true)
                        .setDragRate(0.4f)
                        .setEnableOverScrollBounce(true)
                        .setDisableContentWhenLoading(false)
        );

        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) ->
                new ClassicsHeader(context)
                        .setTextSizeTitle(12)
                        .setEnableLastTime(false)
                        .setDrawableSize(15)
                        .setDrawableMarginRight(6)
                        .setSpinnerStyle(SpinnerStyle.Translate)
                        .setFinishDuration(500));

        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) ->
                new ClassicsFooter(context).setTextSizeTitle(12)
                        .setFinishDuration(500)
                        .setDrawableSize(15)
                        .setDrawableMarginRight(8)
                        .setSpinnerStyle(SpinnerStyle.Translate));

        // 日志框架
        Logger.addLogAdapter(new AndroidLogAdapter() {

            @Override
            public boolean isLoggable(int priority, String tag) {
                return BuildConfig.DEBUG;
            }

        });
    }


    public static MetaEraApplication sInstance;

    public static IWXAPI sWxApi;

//    private final String
//            AppSecret = "533892b8bd8e632282938c196bed75f1",
//            AppID     = "wx02c385252bbe1504";


    @Override
    public void onCreate() {
        super.onCreate();

        sInstance = this;

        initUM();

        registerToWeChat();

        initializeRxJavaException();

        registerActivityLifecycleCallbacks(ActivityNavigateManager.instance());

    }


    private void initUM() {
        // 友盟
        String UMENG_MESSAGE_SECRET = "583e25a7b22b918cb5ffd7b5e0dc785f",
                APP_KEY = "64c8c3d6a1a164591b5f0768";

        // 微信
        String APP_SECRET = "533892b8bd8e632282938c196bed75f1",
                APP_ID = "wx02c385252bbe1504";

        // 初始化
        // UMConfigure.preInit(this, APP_KEY, BuildConfig.FLAVOR);
        // 判断是否同意隐私协议
        UMConfigure.setLogEnabled(BuildConfig.DEBUG);
        UMConfigure.init(this, APP_KEY, BuildConfig.FLAVOR, UMConfigure.DEVICE_TYPE_PHONE, UMENG_MESSAGE_SECRET);

        // 统计
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.MANUAL);

        // 分享
        PlatformConfig.setWeixin(APP_ID, APP_SECRET);
        PlatformConfig.setWXFileProvider(BuildConfig.APPLICATION_ID + ".fileprovider");

        // 推送
        PushHelper.init(this);

        // 崩溃
//        UMCrash.registerUMCrashCallback(new UMCrashCallback(){
//            @Override
//            public String onCallback(){
//                return"崩溃时register的自定义内容字符串";
//            }
//        });
    }


    private void registerToWeChat() {
        sWxApi = WXAPIFactory.createWXAPI(this, "wx02c385252bbe1504", true);
        sWxApi.registerApp("wx02c385252bbe1504");
    }


    private void initializeRxJavaException() {
        try {
            RxJavaPlugins.setErrorHandler(throwable ->
                    Log.e("MetaEraApplication", throwable.getMessage()));

        } catch (Exception e) {
            Log.e("MetaEraApplication", e.getMessage());
        }
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

}
